<?php
include_once("../config.php");
$uid = $_GET['uid'];
$tel = $_GET['tel'];
$dizhi = $_GET['dizhi'];
$lxr = $_GET['lxr'];
//$bm = $_GET['bm'];
$bmm = $_GET['bmm'];
//$xm = $_GET['xm'];
$xmm = $_GET['xmm'];
//$gz = $_GET['gz'];
$gzm = $_GET['gzm'];
$yysj = $_GET['yysj'];
$img = $_GET['img'];
$gzms = $_GET['gzms'];
$t=time();
//$wxid = $_GET['wxid'];
$uid = str_ireplace($search, '', $uid);
$gzms = str_ireplace($search, '', $gzms);
$lxr = str_ireplace($search, '', $lxr);
$dizhi = str_ireplace($search, '', $dizhi);
$tel = str_ireplace($search, '', $tel);
$bmm = str_ireplace($search, '', $bmm);
$xmm = str_ireplace($search, '', $xmm);
$gzm = str_ireplace($search, '', $gzm);
$bm=intval($_GET['bm']);
$wxid=intval($_GET['wxid']);
$xm=intval($_GET['xm']);
$gz=intval($_GET['gz']);


if($wxid ==0){

	$sql="insert into wenti (uid, bmid, bm, dz, lxr, tel, xmid, xm, gzid, gz, ms, img, yysj, tbsj) values 
							('$uid', $bm, '$bmm', '$dizhi', '$lxr', '$tel', $xm, '$xmm', $gz, '$gzm', '$gzms', '$img', '$yysj', $t)";
  // $sql = str_replace(PHP_EOL, '', $sql); 
  // var_dump($sql);exit();
	mysqli_query($con,$sql);
	$id = mysqli_insert_id($con);
	if($id){
		 $result = '{"status":"1","msg":"ok"}';
		 $ok = true;
	}else{
		 $result = '{"status":"0","msg":"提交失败，请重试"}';
		 $ok = false;
	}
	echo $result;
}else{
	$sql = "update wenti set bmid=$bm,bm='$bmm',dz='$dizhi',lxr='$lxr',tel='$tel',xmid=$xm,xm='$xmm',
			gzid=$gz,gz='$gzm',ms='$gzms',img='$img',yysj='$yysj',tbsj=$t,status=11 where id=$wxid";
	mysqli_query($con,$sql);
	if(mysqli_affected_rows($con)){
		 $result = '{"status":"1","msg":"ok"}';
		 $ok = true;
	}else{
		 $result = '{"status":"0","msg":"修改失败，请重试"}';
		 $ok = false;
	}
	echo $result;
}


if($ok){
	include_once("jssdk.php");
	$jssdk = new JSSDK($appid, $secretkey);
	$accesstoken=$jssdk->getAccessToken();
	$sql = "select ming from user where openid='$uid'";
	$requ = mysqli_query($con,$sql);
	$rs=mysqli_fetch_array($requ);
	$shr = $rs['ming'];
	mysqli_free_result($requ);
	$sql = "select openid from user where juese=2 and status=1";
	$requ = mysqli_query($con,$sql);
	//$u=array();
	while($rs=mysqli_fetch_array($requ)){
		$u = $rs['openid'];
		$data=array(
			"touser"=>$u,
			"page"=>"/pages/glysh/glysh?mode=xdsh&d=0&id=$id",
			"template_id"=>"WFWgUVkLuYcRcDJewxMEgDuwP0e4uvC6E2cnjQAkTX4",
			"miniprogram_state"=>"formal",
			"data"=>array(
				"thing2"=>array(
					"value"=>$xmm."维修申请"
				),
				"thing6"=>array(
					"value"=>$gzm
				),
				"name7"=>array(
					"value"=>$shr
				),
				"date8"=>array(
					"value"=>date("Y年m月d日 H:i",time())
				),
				"thing5"=>array(
					"value"=>"请尽快审核。谢谢"
				)
			)
		);
		$data=json_encode($data);
		$url="https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=$accesstoken";
		$r=https_request($url,$data);
		$myfile = fopen("send.log", "a");
		fwrite($myfile, date("Y-m-d H:i:s",time()));
		fwrite($myfile, "\r\n");
		fwrite($myfile, "新维修申请，提报审核");
		fwrite($myfile, "\r\n");
		fwrite($myfile,  var_export($data,true));
		fwrite($myfile, "\r\n");
		fwrite($myfile,  var_export($r,true));
		fwrite($myfile, "\r\n\r\n");
		fclose($myfile);
	}
	mysqli_free_result($requ);
	//$u = json_encode($u);
	//$href = $url."mini/dingyuexiaoxi/index.php?mode=wxsp&user=$u";
	//httpGet($href);
}


function httpGet($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_URL, $url);
    $res = curl_exec($curl);
    curl_close($curl);
    return $res;
}

function https_request($url, $data = null){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)) {
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}




?>